package com.tedu.weibo.controller;

import com.tedu.weibo.mapper.IUserMapper;
import com.tedu.weibo.pojo.dto.UserLoginDTO;
import com.tedu.weibo.pojo.dto.UserRegDTO;
import com.tedu.weibo.pojo.entity.User;
import com.tedu.weibo.pojo.vo.UserVO;
import com.tedu.weibo.result.JsonResult;
import com.tedu.weibo.result.StatusCode;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import javax.servlet.http.HttpSession;
import java.util.Date;

//@Controller
@RestController
@RequestMapping("/v1/users")//请求路径中相同的部分提取到这里
@Api(tags = "用户模块")
public class UserController {
    @Autowired
    private IUserMapper iUserMapper;
    //@RequestMapping(value = "/v1/user/reg",method = RequestMethod.POST)
    @ApiOperation("用户注册")
    @PostMapping("/reg")
    public JsonResult register(@RequestBody UserRegDTO userRegDTO){
        System.out.println(userRegDTO);
        if(userRegDTO.getNickname()==null||userRegDTO.getPassword()==null||userRegDTO.getUsername()==null){
            return new JsonResult(StatusCode.NAME_OR_PASSWORD_ISNULL);
        }
        User userVerify = iUserMapper.verify(userRegDTO.getUsername());
        if(userVerify!=null){
            return new JsonResult(StatusCode.USERNAME_EXISTS);//表示用户已存在
        }
        User user = new User();
        BeanUtils.copyProperties(userRegDTO,user);
        user.setCreated(new Date());
        iUserMapper.insert(user);

        return JsonResult.success();

    }

    @ApiOperation("用户登录")
    @PostMapping("/login")
    public JsonResult login(@RequestBody @Validated UserLoginDTO userLoginDTO, @ApiIgnore HttpSession session){

        System.out.println(userLoginDTO);
        /*if(userLoginDTO.getUsername()==null||userLoginDTO.getUsername().isEmpty()){
            //throw new RuntimeException("用户名不能为空");
            throw new IllegalArgumentException("用户名不能为空");
        }*/
        User user = iUserMapper.verify(userLoginDTO.getUsername());
        System.out.println(user);
        if(user==null){
            return new JsonResult(StatusCode.USERNAME_ERROR);//用户名错误
        }
        //！！！注意：对于引用类型==比较的是地址，若比较内容用equals！！！
        if(!user.getPassword().equals(userLoginDTO.getPassword())){
            System.out.println("密码错误");
            return new JsonResult(StatusCode.PASSWORD_ERROR);//密码错误

        }
        session.setAttribute("user",user);//(sessionID,信息对象）
                                            //"C65216155B79A6EE8CB2CFD7AC7608D2",user1
                                            //"C65216155DJA5DL6AS8CB2CFD7AC7608",user2
                                            //...


        return JsonResult.success();//登录成功
    }

    @ApiOperation("验证是否登录")
    @GetMapping("/currentUser")
    public JsonResult currentUser(@ApiIgnore HttpSession session){
        User user = (User)session.getAttribute("user");
        if(user==null){
            System.out.println("没有登录");
            return new JsonResult(StatusCode.NOT_LOGIN);
        }else{
            System.out.println("欢迎你"+user.getUsername());
            UserVO userVO = new UserVO();
            BeanUtils.copyProperties(user,userVO);
            return JsonResult.success(userVO);
        }
    }

    @ApiOperation("退出登录")
    @GetMapping("/logout")
    public void logout(@ApiIgnore HttpSession session){

        session.removeAttribute("user");
        System.out.println("已退出登录");

    }
}
